﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class PlayerMove : MonoBehaviour {
    //单例
    public static PlayerMove _instance;
   
    //存放当前的主角位置
    public Vector3 PlayerPos;
    private List<Vector3> PosArr;
    //主角是否死亡
    public bool isDie;

    private GameObject GameOverMenu;
    
    //得分
    private GameObject ScoreText;
    public static int score = 0;
    //金币
    private GameObject GoldText;
    public static int gold = 100;
    //人物动画
    public Animator Jump;

    private float timer = 1f;
    void Awake()
    {
        _instance = this;
        GameOverMenu = GameObject.Find("GameOverMenu");

        Jump = this.GetComponent<Animator>();
    }
    // Use this for initialization
    void Start () {
        PosArr = new List<Vector3>();
        PosArr.Add(this.transform.position);

        ScoreText = GameObject.Find("Canvas/Score/Text");
        GoldText = GameObject.Find("Canvas/Money/Text");
        score = 0;
    }
	
	// Update is called once per frame
	void Update () {
       
        //代替左右点击屏幕移动
        if (Input.GetKeyDown(KeyCode.A))
        {
            this.transform.position += new Vector3(-1, 0, 0);
            if (PosArr.Count >= 5)
            {
                PosArr.Remove(PosArr[0]);

                PosArr.Add(this.transform.position);
            }
        }
        if (Input.GetKeyDown(KeyCode.D))
        {
            this.transform.position += new Vector3(0, 0, 1);
        }

        //实时更新分数、金币
        PlayerPrefs.SetInt("score", score);
        ScoreText.GetComponent<Text>().text = score.ToString();
        PlayerPrefs.SetInt("gold", gold);
        GoldText.GetComponent<Text>().text = gold.ToString();
        PlayerPrefs.Save();

        timer -= Time.deltaTime;
        if (timer <= 0)
        {
            //Animator
            Jump.SetBool("Jump", false);
            Debug.Log("asa");
            timer = 1f;
        }
    }

#region 点击控制主角移动 && 分数累加
    /// <summary>
    /// 点击左右屏幕使得角色移动
    /// </summary>
    public void LeftBtn()
    {
        this.transform.position += new Vector3(-1, 0, 0);
        if (PosArr.Count >= 5)
        {
            PosArr.Remove(PosArr[0]);

            PosArr.Add(this.transform.position);
        }
        //foreach (var item in PosArr)
        //{
        //    Debug.Log(item);
        //}
        Jump.SetBool("Jump",true);
        score++;
    }

    public void RightBtn()
    {
        this.transform.position += new Vector3(0, 0, 1);
        if (PosArr.Count >= 5)
        {
            PosArr.Remove(PosArr[0]);

            PosArr.Add(this.transform.position);
        }
        Jump.SetBool("Jump", true);
        score++;
    }

    #endregion

    //碰撞到底部背景GameOver
    void OnCollisionEnter(Collision collision)
    {
      
        if (collision.gameObject.tag == "die")
        {
            //TO DO 游戏结束处理
            //Time.timeScale = 0;
            //Debug.Log(collision.gameObject.name);
            Debug.Log("游戏结束"+ isDie);
            isDie = true;
           // PlayerPos = PosArr[2];
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.tag == "gold")
        {
            Destroy(other.gameObject);
            gold++;
        }
    }
}
